package org.gruposp2p.dnie.ui.panels;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gruposp2p.dnie.model.DNIeDocumentToSignOption;
import org.gruposp2p.dnie.model.DNIeOptionSelectionType;
import org.gruposp2p.dnie.model.DNIeSignedDocumentOptionSelected;

/**
 *
 * @author jj
 */
public class OptionsPanel extends javax.swing.JPanel implements ItemListener {

    private static Logger logger = LoggerFactory.getLogger(OptionsPanel.class);

    private Collection<DNIeSignedDocumentOptionSelected> optionsSelected;
    private Collection<DNIeDocumentToSignOption> optionsToSelect;
    private ButtonGroup group;

    public OptionsPanel () {
        MigLayout layout = new MigLayout();
        this.setLayout(layout);
    }

    public Collection<DNIeSignedDocumentOptionSelected> getOptions () {
        return optionsSelected;
    }

    public void setSigned (boolean isSigned) {
        Enumeration<AbstractButton> radioButtons = group.getElements();
        while (radioButtons.hasMoreElements()) {
            AbstractButton button = radioButtons.nextElement();
            button.setEnabled(!isSigned);
        }
    }

    public void repaintOptions (Collection<DNIeDocumentToSignOption> optionsToSelect,
            DNIeOptionSelectionType optionSelectionType) {
        this.optionsToSelect = optionsToSelect;
        optionsSelected = null;
        removeOptions();
        JLabel messageLabel = new JLabel();
        switch (optionSelectionType) {
            case EXCLUSIVE:
                messageLabel.setText("<html><b>Seleccione una de las siguientes opciones</b></html>");
                break;
            case INCLUSIVE:
                messageLabel.setText("<html><b>Seleccione al menos una de las siguientes opciones</b></html>");
                break;
        }
        this.add(messageLabel, "wrap");
        group = new ButtonGroup();
        Enumeration abstractButton = group.getElements();
        for (DNIeDocumentToSignOption option : optionsToSelect) {
            JRadioButton choice = new JRadioButton(option.getContent());
            choice.setActionCommand(option.getId().toString());
            choice.addItemListener(this);
            group.add(choice);
            this.add(choice, "wrap");
        }
        if (optionsToSelect.size() > 0) {
            Container container = this.getTopLevelAncestor();
            if (container instanceof JDialog) {
                ((JDialog)this.getTopLevelAncestor()).pack();
            }
        } else removeOptions();
        revalidate();
        //frame.pack();
    }

    private void removeOptions () {
        Component[] components = getComponents();
        for (Component component : components) {
            if (component instanceof JRadioButton) {
                this.remove(component);
            } else if (component instanceof JLabel) {
                this.remove(component);
            }
        }
        revalidate();
    }

    public boolean isDataValid () {
        boolean result = false;
        if (optionsSelected != null && !optionsSelected.isEmpty()) {
            result = true;
        }
        return result;
    }

    public void itemStateChanged(ItemEvent ev) {
        AbstractButton button = (AbstractButton) ev.getItemSelectable();
        optionsSelected = new ArrayList<DNIeSignedDocumentOptionSelected>();
        for (DNIeDocumentToSignOption option : optionsToSelect) {
            if (option.getId().toString().equals(button.getActionCommand())) {
                DNIeSignedDocumentOptionSelected optionSelected =
                    new DNIeSignedDocumentOptionSelected();
                optionSelected.setSelected(true);
                optionSelected.setDNIeDocumentToSignOptionId(option);
                optionsSelected.add(optionSelected);
            }
        }
    }

}
